//  https://ac.nowcoder.com/acm/problem/226514

#include<iostream>
#include<cstring>
using namespace std;

int n, m;
const int N = 1010;
int v[N], w[N];
int dp[N][N];

int main()
{
    cin >> n >> m;
    for (int i = 1;i <= n;i++) cin >> v[i] >> w[i];

    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
        }
    }
    cout << dp[n][m] << endl;

    memset(dp, -0x3f3f3f3f, sizeof dp);
    dp[0][0] = 0;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 0;j <= m;j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j >= v[i])
                dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
        }
    }
    if (dp[n][m] < 0) cout << 0 << endl;
    else cout << dp[n][m] << endl;
    return 0;
}